package tmhprediction.test;

import java.io.File;
import java.util.HashMap;

import tmhprediction.classification.SecondClassifier;
import tmhprediction.classification.TMHClassifier;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.ConverterUtils.DataSource;

public class PrepareArffForSVM3 {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception
	{
		String modelp = args[0];		//readily trained models,moriginal on impSetOut									/home/jonas/Desktop/pp1/FINAL/output/secondClassifier
		String svm3ArffBasep = args[1];	// the base files that will be extended with the confidence base on the prediction by svm2 		/home/jonas/Desktop/pp1/FINAL/impSetOutSVM3_PRELIM
		String svm2ArffBasep = args[2]; // the three files on which svm2 tests											/home/jonas/Desktop/pp1/FINAL/impSetOut 
		String outFolder = args[3];		// folder in which the final enriched arff files will be saved					/home/jonas/Desktop/pp1/FINAL/impSetOutSVM3
		int windowsize = Integer.valueOf(args[4]);
		
		doIt(modelp, svm3ArffBasep, svm2ArffBasep, outFolder, 0, 1, windowsize);
		doIt(modelp, svm3ArffBasep, svm2ArffBasep, outFolder, 1, 2, windowsize);
		doIt(modelp, svm3ArffBasep, svm2ArffBasep, outFolder, 2, 0, windowsize);
	}
	
	static void doIt(String modelp, String svm3ArffBasep, String svm2ArffBasep, String outFolder, int indexTrain, int indexTest, int windowsize) throws Exception
	{
		SecondClassifier sc = new SecondClassifier(1500);
		sc.initializeTrainedClassifier(modelp+"/" + indexTrain + "_model.svm", svm2ArffBasep + "/features_manual_" + indexTest + ".arff");
		HashMap<String, double[]> resM = sc.getClassifierWrapper().createResultMap();
		Instances svm3BaseSet = DataSource.read(svm3ArffBasep + "/features_manual_" + indexTest + ".arff");
		Instances svm3BaseSetEnriched = TMHClassifier.insertConfidence3SVM_window(resM,  svm3BaseSet, windowsize);
		
		ArffSaver saver = new ArffSaver();
		saver.setInstances(svm3BaseSetEnriched);
		saver.setFile(new File(outFolder + "/features_manual_" + indexTest + ".arff"));
		saver.setDestination(new File(outFolder + "/features_manual_" + indexTest + ".arff"));
		saver.writeBatch();
		
	}

}
